package com.lwj.liwanjia.adapter.fabu;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.fabu.FabuTabBean;
import com.lwj.liwanjia.config.OnItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/13.
 */

public class FabuTabAdapter extends RecyclerView.Adapter<FabuTabAdapter.MyViewHolder> {

    private List<FabuTabBean> list;
    private LayoutInflater inflater;
    private OnItemClickListener listener;
    private Context mContext;
    private boolean flag;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public FabuTabAdapter(Context context, List<FabuTabBean> list) {
        this.list = list;
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        flag = true;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.select_item1, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.tvSelect.setText(list.get(position).getTabName());
        if (list.get(position).isCheck()) {
            holder.tvSelect.setBackgroundResource(R.drawable.tab_shape);
            holder.tvSelect.setTextColor(Color.WHITE);
        } else {
            holder.tvSelect.setBackgroundResource(R.drawable.tab_shape1);
            holder.tvSelect.setTextColor(mContext.getResources().getColor(R.color.colorC4C4C4));
        }
        if (listener != null&&flag) {
            holder.tvSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.OnItemClick(v, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_select)
        TextView tvSelect;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setClickEnable(boolean flag){
        this.flag=flag;
        notifyDataSetChanged();
    }
}
